package com.orangelife.arenasdk.util;

import android.util.Log;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * <p>
 * Created by 吴格非 on 2016-10-9.
 * <p>
 *
 * @author 吴格非
 * @since v1.0.0
 */

public class SignUtil {

    private static final String TAG = SignUtil.class.getSimpleName();

    public static String genSign(Map<String, Object> params) {
        Map<String, Object> map = getParByAsII(params);
        StringBuilder sb = new StringBuilder();
        for (String key : map.keySet()) {
            if (sb.length() > 0) {
                sb.append('&');
            }
            sb.append(key);
            sb.append('=');
            sb.append(params.get(key));
        }
//        sb.append("key=");
//        sb.append(Constants.API_KEY);
        Log.e(TAG, "sign param=" + sb.toString());
        String sign = MD5Util.getMessageDigest(sb.toString().getBytes()).toUpperCase();
        Log.e(TAG, sign);
        return sign;
    }

    public static Map<String, Object> getParByAsII(Map<String, Object> paMap) {
        Map<String, Object> map = new TreeMap<>(
                new Comparator<String>() {
                    public int compare(String firstKey, String secTkey) {
                        // 升序序排序
                        return firstKey.compareTo(secTkey);
                    }
                });
        for (Map.Entry<String, Object> entry : paMap.entrySet()) {
            map.put(entry.getKey(), entry.getValue());
        }

        return map;
    }
}
